<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <div class="tools-select">
          <el-row>
            <el-col>
              <el-form
                :inline="true"
                :model="form"
                @submit.native.prevent="handleQuery()"
              >
                <el-form-item>
                  <el-input
                    v-model="form.nickName"
                    placeholder="昵称"
                    size="mini"
                    clearable
                    status-icon
                    @clear="handleQuery"
                  />
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="primary"
                    size="mini"
                    icon="el-icon-search"
                    native-type="submit"
                    >查询
                  </el-button>
                  <el-button
                    type="info"
                    size="mini"
                    icon="el-icon-plus"
                    @click="handleAdd"
                    v-if="$store.getters.roles.includes('SYS_ROLE_ADD')"
                    >新增
                  </el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
      </div>
      <!-- 表格区 -->
      <el-table size="mini" stripe :data="tableData" border>
        <el-table-column
          align="center"
          label="序号"
          type="index"
          width="50px"
        />
        <el-table-column align="center" label="编号" prop="id" />
        <el-table-column align="center" label="昵称" prop="nickName" />
        <el-table-column align="center" label="公司名" prop="companyName" width="300"/>
        <el-table-column align="center" label="手机号" prop="phone" />
        <el-table-column align="center" label="邮箱" prop="email" />
        <el-table-column align="center" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="创建人" prop="creatorName" />
        <el-table-column align="center" label="操作" width="300">
          <template v-slot="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-view"
              @click="handleView(scope.row.id, scope.row.nickName)"
              >详细信息
            </el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row.id)"
              v-if="$store.getters.roles.includes('SYS_USER_EDIT')"
              >修改
            </el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-s-custom"
              @click="handleUserEdit(scope.row.id)"
              v-if="$store.getters.roles.includes('SYS_USER_DISPENSE')"
              >分配角色
            </el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="handleDel(scope.row.id)"
              v-if="$store.getters.roles.includes('SYS_USER_DELETE')"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        :current-page="this.page.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <EditDialog ref="edit" @onCallBack="handleQuery" />
    <SysUserDialog ref="sysUser" />
    <ViewDialog ref="view" />
  </div>
</template>

<script>
import { getPage, deleteById } from "@/api/sysUser";
import SysUserDialog from "./components/SysUserDialog";
import EditDialog from "./components/EditDialog";
import ViewDialog from "./components/ViewDialog";

export default {
  components: { EditDialog, ViewDialog, SysUserDialog },
  data() {
    return {
      tableData: [],
      page: {
        pageSize: 10,
        pageNum: 1,
        total: undefined,
      },
      form: {
        nickName: "",
      },
    };
  },
  mounted() {
    this.handleQuery();
  },
  methods: {
    handleQuery() {
      getPage({
        pageSize: this.page.pageSize,
        pageNum: this.page.pageNum,
        nickName: this.form.nickName,
      }).then((res) => {
        this.tableData = res.data.records;
        this.page.total = res.data.total;
      });
    },
    handleSizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.handleQuery();
    },
    handleCurrentChange(pageNum) {
      this.page.pageNum = pageNum;
      this.handleQuery();
    },
    handleAdd() {
      this.$refs["edit"].onShow();
    },
    handleEdit(id) {
      this.$refs["edit"].onShow(id);
    },
    handleView(id, name) {
      this.$refs["view"].onShow(id, name);
    },
    handleDel(id) {
      deleteById(id).then((res) => {
        console.log(res);
        if (res.code === 20000) {
          this.handleQuery();
          this.$message({
            message: "操作成功",
            type: "success",
          });
        }
      });
    },
    handleUserEdit(id) {
      this.$refs["sysUser"].onShow(id);
    },
  },
};
</script>
<style scoped>
.el-pagination {
  float: left;
  padding-top: 20px;
  padding-bottom: 20px;
}

.tools-select {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 10px;
  border: 1px;
  border-radius: 5px;
}
</style>
